package com.happyscan138.makefun;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.happyscan138.makefun.Fragment.MyCollectionQuestuion;
import com.happyscan138.makefun.Fragment.MyCollectionVideo;
import com.happyscan138.makefun.adapter.MyFragmentPagerAdapter;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/12/8.
 */

public class myCollection extends AppCompatActivity implements View.OnClickListener {
    private TextView textViewQuestion;
    private TextView textViewvideo;
    private ArrayList<Fragment> fragmentsList;
    private ViewPager mPagerMyDiscuss;
    private ImageView imagetab;//下划线
    private int position_one;//初始位移
    private int pWidth;//宽度
    private int offset ;// 图片偏移量
    public final static int num = 2;
    private int currIndex = 0;
    private Fragment question;
    private Fragment video;
    private ImageView back;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_collection);
        initView();
        InitViewPager();
        InitWidth();
        TranslateAnimation animation = new TranslateAnimation(0, offset, 0, 0);
        animation.setFillAfter(true);
        animation.setDuration(300);
        imagetab.startAnimation(animation);
    }
    public void initView(){
        back=(ImageButton)findViewById(R.id.my_collection_back);
        back.setOnClickListener(this);
        imagetab=(ImageView)findViewById(R.id.my_collection_tab);
        textViewQuestion=(TextView)findViewById(R.id.collection_text_question);
        textViewvideo=(TextView)findViewById(R.id.collection_text_video);
        textViewQuestion.setOnClickListener(this);
        textViewvideo.setOnClickListener(this);

    }
    private void InitViewPager( ) {
        mPagerMyDiscuss = (ViewPager)findViewById(R.id.collection_viewpager);
        fragmentsList = new ArrayList<Fragment>();
        question = new MyCollectionQuestuion();
        video = new MyCollectionVideo();
        fragmentsList.add(question);
        fragmentsList.add(video);

        mPagerMyDiscuss.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentsList));
        mPagerMyDiscuss.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                Animation animation = null;
                switch (position) {
                    case 0:
                        if (currIndex == 1) {
                            animation = new TranslateAnimation(position_one, offset, 0, 0);
                        }

                        break;
                    case 1:
                        if (currIndex == 0) {
                            animation = new TranslateAnimation(offset, position_one, 0, 0);
                        }

                        break;
                }
                currIndex = position;
                animation.setFillAfter(true);
                animation.setDuration(300);
                imagetab.startAnimation(animation);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
        });
        mPagerMyDiscuss.setCurrentItem(0);
    }
    private void InitWidth( ) {
        imagetab = (ImageView) findViewById(R.id.my_collection_tab);
        BitmapDrawable bd = (BitmapDrawable)getResources().getDrawable(R.mipmap.up_tab);
        Bitmap bmp = bd.getBitmap();
        pWidth = bmp.getWidth();
        DisplayMetrics dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;
        offset = (int) ((screenW / num -pWidth)) / 2;//偏移量
        int avg = (int) (screenW / num);
        position_one = avg+offset;//起始位置

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.my_collection_back:
                finish();
                break;
            case R.id.collection_text_question:
                mPagerMyDiscuss.setCurrentItem(0);
                break;
            case R.id.collection_text_video:
                mPagerMyDiscuss.setCurrentItem(1);
                break;
        }
    }


}
